package de.szut.sudoku.main;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import de.szut.sudoku.difficulty.DifficultyContainer;
import de.szut.sudoku.game.GameModel;
import de.szut.sudoku.gui.MainFrame;

/**
 * Main of the program and controller between model and views
 * 
 * @author dqi12huisinga
 * 
 */

public class GameController {
	private static GameController main;

	private DifficultyContainer dContainer;

	private GameModel game;
	private UserInterface ui;

	private String configPath;
	private Properties configuration;

	public String getConfigPath() {
		return configPath;
	}

	public void setConfigPath(String configPath) {
		this.configPath = configPath;
	}

	public Properties getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Properties configuration) {
		this.configuration = configuration;
	}

	public GameModel getGame() {
		return game;
	}

	public void setGame(GameModel game) {
		this.game = game;
	}

	/**
	 * Singleton for the instance of GameController
	 * 
	 * @return
	 */
	public static GameController getInstance() {
		if (main == null) {
			main = new GameController();
			main.callInit();
		}
		return main;
	}

	/**
	 * Main of the program
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		getInstance();
	}

	public DifficultyContainer getdContainer() {
		return dContainer;
	}

	public void setdContainer(DifficultyContainer dContainer) {
		this.dContainer = dContainer;
	}

	/**
	 * Later-called constructor of the class
	 */
	private void callInit() {
		// loads the configuration file
		configuration = new Properties();
		configPath = "data/config.cfg";
		try {
			configuration.load(new FileInputStream(configPath));
		} catch (IOException e) {	
			System.out.println("Config couldn't be loaded!");
		}

		// instantiates a DifficultyContainer and loads the last difficulty from
		// the config
		setdContainer(new DifficultyContainer());
		dContainer.setCurrentDifficulty(dContainer
				.getDifficultyFromName(configuration
						.getProperty("game.difficulty")));

		// starts the game model
		game = new GameModel();
		// starts the UI
		// GUI: (UserInterface) MainFrame.getInstance();
		// CUI: (UserInterface) CUIController.getInstance();
		ui = (UserInterface) MainFrame.getInstance();
		game.addObserver(ui);
		ui.startUI();
	}

}
